safe_str*() functions check their destination argument is a
authorkfraser@localhost.localdomain <kfraser@localhost.localdomain>
Thu, 1 Feb 2007 13:44:35 +0000 (13:44 +0000)
committerkfraser@localhost.localdomain <kfraser@localhost.localdomain>
Thu, 1 Feb 2007 13:44:35 +0000 (13:44 +0000)
character-array type. Fix two bad callers.
Signed-off-by: Keir Fraser <keir@xensource.com>
xen/arch/x86/dmi_scan.c
xen/common/kexec.c
xen/include/xen/string.h

index 2d49d412fc9d4078255da2123031c7f97562b665..b867febc1e209a8c907ca66cf2afbe0a20c74023 100644 (file)
@@ -159,7 +159,7 @@ static void __init dmi_save_ident(struct dmi_header *dm, int slot, int string)
                return;
        dmi_ident[slot] = alloc_bootmem(strlen(p)+1);
        if(dmi_ident[slot])
-               safe_strcpy(dmi_ident[slot], p);
+               strlcpy(dmi_ident[slot], p, strlen(p)+1);
        else
                printk(KERN_ERR "dmi_save_ident: out of memory.\n");
 }
index 04a1468d3a718dbeb7d1d8113fb9db041ce90e36..9e6e1858a413c0d697b5152149ad967d10369ff5 100644 (file)
@@ -131,7 +131,7 @@ __initcall(register_crashdump_trigger);
 
 static void setup_note(Elf_Note *n, const char *name, int type, int descsz)
 {
-    safe_strcpy(ELFNOTE_NAME(n), name);
+    strlcpy(ELFNOTE_NAME(n), name, INT_MAX);
     n->namesz = strlen(name);
     n->descsz = descsz;
     n->type = type;
index d0c23a14116d159199a434f9ad279c38f102b6fe..f26b9949b207793013c762df6fa3c6e62bd538e8 100644 (file)
@@ -82,8 +82,16 @@ extern void * memchr(const void *,int,__kernel_size_t);
 }
 #endif
 
+#define is_char_array(x) __builtin_types_compatible_p(typeof(x), char[])
+
 /* safe_xxx always NUL-terminates and returns !=0 if result is truncated. */
-#define safe_strcpy(d, s) (strlcpy(d, s, sizeof(d)) >= sizeof(d))
-#define safe_strcat(d, s) (strlcat(d, s, sizeof(d)) >= sizeof(d))
+#define safe_strcpy(d, s) ({                    \
+    BUILD_BUG_ON(!is_char_array(d));            \
+    (strlcpy(d, s, sizeof(d)) >= sizeof(d));    \
+})
+#define safe_strcat(d, s) ({                    \
+    BUILD_BUG_ON(!is_char_array(d));            \
+    (strlcat(d, s, sizeof(d)) >= sizeof(d));    \
+})
 
 #endif /* _LINUX_STRING_H_ */